import xml.sax
from xml.sax.handler import ContentHandler 

class game_info_handler_server(ContentHandler):
    def __init__(self):
        self.currentElement = None
    
    def startElement(self, name, attrs):
        if name == "tank_game_info_request":
            self.request_type = "info_request"
        elif name == "tank_game_join_request":
            self.request_type = "join_request"
            self.callsign = attrs['callsign']

class game_info_handler_client(ContentHandler):
    def __init__(self):
        self.game = {}
        self.currentElement = None
        self.request_type = ""
    
    def startElement(self, name, attrs):
        if name == "tank_game_info":
            self.game = {}
            self.request_type = "game_info"
            self.game['name'] = attrs['name']
            self.game['port'] = attrs['port']
            self.game['players'] = attrs['players']
            self.game['open_spots'] = attrs['open_spots']
            
        elif name == "tank_game_join_request":
            self.request_type = "join_request"
            self.callsign = attrs['callsign']
        
        elif name == "tank_game_join_response":
            self.request_type = name
            self.answer = attrs['answer']
            self.game_port = attrs['port']

class game_setup_handler_server(ContentHandler):
    def __init__(self):
        self.currentElement = None
    
    def startElement(self, name, attrs):
        if name == "tank_game_ready":
            self.request_type = "tank_game_ready"
            self.player_id = int(attrs['player_id'])

class game_setup_handler_client(ContentHandler):
    def __init__(self):
        self.currentElement = None
        self.players = []
    
    def startElement(self, name, attrs):
        if name == "tank_game_start":
            self.request_type = "tank_game_start"
        elif name == "player" and self.request_type == "tank_game_start":
            newPlayer = {}
            newPlayer['callsign'] = attrs['callsign']
            newPlayer['player_id'] = int(attrs['player_id'])
            pos = attrs['start_pos']
            x, y = pos.split(",")
            x = int(x)
            y = int(y)
            newPlayer['start_pos'] = (x, y)
            self.players.append(newPlayer)
            

class game_loop_handler_server(ContentHandler):
    def __init__(self):
        self.currentElement = None
        self.events = []
    
    def startElement(self, name, attrs):
        if name == "ack":
            self.request_type = "ack"
        elif name == "player_update":
            self.request_type = "update"
        elif name == "event" and self.request_type == "update":
            self.events.append(attrs)
            

class game_loop_handler_client(ContentHandler):
    def __init__(self):
        self.currentElement = None
        self.events = []
    
    def startElement(self, name, attrs):
        if name == "update":
            self.request_type = "update"
        elif name == "event" and self.request_type == "update":
            self.events.append(attrs)
